package com.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.OnClick;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.InputType;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.api.param.LoginParam;
import com.api.param.MobileParam;
import com.api.viewModel.LoginVM;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.utils.ActivityUtil;
import com.example.apple.ddhr_new.utils.StringUtil;
import com.example.apple.ddhr_new.utils.ToastUtil;
import com.example.lzbase.BaseActivity;
import com.example.lzbase.BaseViewModelActivity;

public class ForgetPwdActivity extends BaseViewModelActivity<LoginVM> {

    @BindView(R.id.et_phone)
    EditText phone;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.btn_mima)
    ImageView btnMima;
    @BindView(R.id.btn_denglu)
    Button btnDenglu;
    @BindView(R.id.tv_sendcode)
    TextView tvSendcode;
    @BindView(R.id.ed_code)
    EditText edCode;
    private String code;

    @Override
    protected int titleText() {
        return R.string.wang_ji_mi_ma;
    }

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            tvSendcode.setText(millisUntilFinished / 1000 + "s");

        }

        @Override
        public void onFinish() {
            tvSendcode.setEnabled(true);
            tvSendcode.setText("再次发送");

        }
    };

    @Override
    protected void created(@Nullable Bundle savedInstanceState) {

        super.created(savedInstanceState);
        viewModel.code.observe(this, res -> {
            if (res != null) {
                timer.start();
                tvSendcode.setEnabled(!tvSendcode.isEnabled());
                code = res.getResult();
            } else {

                tvSendcode.setEnabled(true);
                tvSendcode.setText("再次发送");
            }
        });
        viewModel.setPwd.observe(this,res->{

            finish();
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget_pwd;
    }

    @OnClick(R.id.btn_mima)
    public void onClick() {

        btnMima.setSelected(!btnMima.isSelected());

        if (btnMima.isSelected()) {
            etPwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        } else {
            etPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }

    }


    @OnClick(R.id.tv_sendcode)
    public void sendCode() {
        if (phone.getText().toString().trim().length() == 0) {

            ToastUtil.toast(this, R.string.qing_shu_ru_shou_ji_hao);
            return;
        }
        if (!StringUtil.isMobileNO(phone.getText().toString().trim())) {

            ToastUtil.toast(this, R.string.qing_shu_ru_zheng_que_shou_ji_hao);
            return;
        }

        addSubscription(viewModel.getMessageCode(new MobileParam("mobi" + phone.getText().toString().trim())));


    }

    @OnClick(R.id.btn_denglu)
    public void register() {
        if (phone.getText().toString().trim().length() == 0) {

            ToastUtil.toast(this, R.string.qing_shu_ru_shou_ji_hao);
            return;
        }
        if (!StringUtil.isMobileNO(phone.getText().toString().trim())) {

            ToastUtil.toast(this, R.string.qing_shu_ru_zheng_que_shou_ji_hao);
            return;
        }
        if(code==null){
            ToastUtil.toast(this, R.string.qing_huo_qu_yan_zheng_ma);
            return;
        }


        if (edCode.getText().toString().length()==0){
            ToastUtil.toast(this, R.string.qing_shu_ru_yan_zheng_ma);
            return;
        }
        if (!edCode.getText().toString().equals(code)){
            ToastUtil.toast(this, R.string.yan_zheng_ma_error);
            return;
        }



        if (etPwd.getText().toString().length()==0){
            ToastUtil.toast(this, R.string.qing_shu_ru_mi_ma);
            return;
        }
         LoginParam loginParam=new LoginParam(phone.getText().toString().trim(),"");
        loginParam.newpassword = etPwd.getText().toString();
        addSubscription(viewModel.resetPwd(loginParam));

    }
}
